<template>
	<view class="content">
		<view class="top">
			<uni-nav-bar title="购买列表" backgroundColor="transparent" :border="false"></uni-nav-bar>
			<view class="box">
				<view class="nav">
					<view class="nav-list" :class="{'nav-list-active':navType==1}" @click="switchNav(1)">
						<text>我的购买</text>
					</view>
					<view class="nav-list" :class="{'nav-list-active':navType==2}" @click="switchNav(2)">
						<text>我的团队</text>
					</view>
				</view>
				<view class="stauts-nav" v-if="navType==1">
					<view class="status-item" v-for="item in typeStatus" :class="{'status-active':currentStatus==item.type}" @click="changeStatus(item)">
						{{item.labal}}
					</view>
				</view>
				<view class="stauts-nav" v-if="navType==2">
					<view class="status-item" v-for="item in teamStatus" :class="{'status-active':currentTeam==item.type}" @click="changeTeamStatus(item)">
						{{item.labal}}
					</view>
				</view>
				<view class="subscription-list">
					<view v-if="navType==1">
						<view v-if="myList.length==0" class="subscription-item empty-box">
							<u-empty mode="list"></u-empty>
						</view>
						<view class="subscription-item" v-for="item in myList">
							<view class="item-top">
								<view class="item-top-img">
									<!-- <view class="status-box" :class="'status-'+ item.status">
										{{item.status==1?'待付款':item.status==2?'已支付':item.status==3?'已到货':"已取消"}}
									</view> -->
									<u--image :src="item.goods_img" width="180rpx" height="250rpx"></u--image>
								</view>
								<view class="item-top-text">
									<view class="text-top">
										<view class="text-top-title">{{item.goods_name}}</view>
										<view class="text-top-time">时间范围：{{item.start_time}}至{{item.end_time}}</view>
									</view>
									<view class="flex-box cancel-box">
										<view class="btn-buy cancel-btn" @click="orderRefundOrder(item)" v-if="item.status==2&&item.is_refund==1">
											我要退款
										</view>
									</view>
									<view class="text-bottom flex-box">
										<view class="left-box">
											<text class="text-bottom-text">收益：</text>
											<text class="text-bottom-amount">¥{{item.earnings}}</text>
										</view>
										<view class="right-box flex-box">
											<!-- 1-待付款 2-已支付 3 已到货 4 已取消 -->
											<view class="btn-buy" @click="gotoPay(item)" v-if="item.status==1">
												去支付
											</view>
											<view class="btn-buy integral-box" v-else-if="item.integral>0">
												积分订单
											</view>
											<!-- {{item.status==1?'待付款':item.status==2?'已支付':item.status==3?'已到货':"已取消"}} -->
										</view>
									
									</view>
								</view>
							</view>
							<view class="item-bottom">
								<view class="item-bottom-left">
									<view class="left-avatar">
										<u-avatar :src="item.avatar" size="30"></u-avatar>
									</view>
									<view class="left-name">
										{{item.nick}}
									</view>
								</view>
								<view class="item-bottom-right" v-if="item.status==2" @click="subscriptionDetail(item)">
									<text>更多详情</text>
									<u-icon name="arrow-right" color="#999999" size="16"></u-icon>
								</view>
								<view class="item-bottom-right" v-if="item.status==1" @click="cancelOrder(item)" >
									<view class="btn-buy cancel-btn" >
										取消订单
									</view>
								</view>
							</view>
						</view>
					</view>
					<view v-else-if="navType==2">
						<view class="subscription-item empty-box" v-if="myteamlist.length == 0">
							<u-empty mode="list"></u-empty>
						</view>
						<view class="subscription-item" v-for="item in myteamlist">
							<view class="item-top">
								<view class="item-top-img">
									<u--image :src="item.goods_img" width="180rpx" height="250rpx"></u--image>
								</view>
								<view class="item-top-text">
									<view class="text-top">
										<view class="text-top-title">{{item.goods_name}}</view>
										<view class="text-top-time">时间范围：{{item.start_time}}至{{item.end_time}}</view>
									</view>
									<view class="text-bottom flex-box">
										<view class="left-box">
											<text class="text-bottom-text">收益：</text>
											<text class="text-bottom-amount">¥{{item.earnings}}</text>
										</view>
										<view class="right-box flex-box">
											<view class="btn-buy"  v-if="item.integral>0">
												积分订单
											</view>
										</view>
									
									</view>
								</view>
							</view>
							<view class="item-bottom">
								<view class="item-bottom-left">
									<view class="left-avatar">
										<u-avatar :src="item.avatar?item.avatar:avatar" size="30"></u-avatar>
									</view>
									<view class="left-name">
										<text class="nick">{{item.nick}} </text>
										<!-- <text class="level">{{item.level==1?'一级成员':'二级成员'}}</text> -->
										<text class="text-title-lv one" v-if="item.level == 1">
											一级成员
										</text>
										<text class="text-title-lv two" v-else>
											二级成员
										</text>
									</view>
								</view>
								<view class="item-bottom-right" @click="subscriptionDetail(item)">
									<text>更多详情</text>
									<u-icon name="arrow-right" color="#999999" size="16"></u-icon>
								</view>
							</view>
						</view>
					</view>
				</view>

			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				navType: 1,
				myList: [],
				myteamlist: [],
				paging: {
					page: 1,
					limit: 10,
				},
				total: null,
				typeStatus:[
					{
						type:2,
						labal:'已付款'
					},
					{
						type:1,
						labal:'待付款'
					},
				],
				teamStatus:[
					{
						type:0,
						labal:'全部'
					},
					{
						type:1,
						labal:'一级成员'
					},
					{
						type:2,
						labal:'二级成员'
					},
				],
				currentStatus:2,
				currentTeam:0,
				settingData:{},
				avatar:'',
				memberHome:{}
			}
		},
		onShow() {
			this.navType = 1
			this.currentStatus = 2
			this.currentTeam = 0
			this.refresh()
			this.settingData = uni.getStorageSync('settingData')
			this.memberHome=uni.getStorageSync('memberHome')
			if(this.settingData){
				this.avatar = this.settingData.image
			}
		},
		onLoad() {
			
		},
		onReachBottom() {
			// console.log('onReachBottom')
			if (this.navType == 1) {
				if (this.myList.length == this.total) return
				this.paging.page++
				this.getMylist()
			} else {
				if (this.myteamlist.length == this.total) return
				this.paging.page++
				// console.log('geMyteamlist')
				this.geMyteamlist('getmore')
			}
		},
		onPullDownRefresh() {
			uni.startPullDownRefresh({
				success: () => this.refresh(),
				complete: () => uni.stopPullDownRefresh()
			})
		},
		methods: {
			// 切换nav
			switchNav(e) {
				console.log(e)
				this.navType = e
				this.currentStatus = 2
				this.refresh()
			},
			// 切换changeStatus
			changeStatus(e) {
				console.log(e)
				this.currentTeam = 0
				this.currentStatus = e.type
				this.refresh()
			},
			// 切换changeTeamStatus
			changeTeamStatus(e) {
				console.log(e)
				this.currentTeam = e.type
				this.refresh()
			},
			// 跳转详情
			subscriptionDetail(item) {
				uni.navigateTo({
					url: `/pages/subscription/subscriptionDetail?order_id=${item.order_id}&type=${this.navType}&avatar=${item.avatar}`
				})
			},
			gotoPay(item){
				uni.navigateTo({
					url: '/pages/index/pay/pay?order_id=' + item.order_id + '&obj=' + JSON.stringify(item)
				})
			},
			// 取消订单
			cancelOrder(item){
				let params = {
					order_id : item.order_id
				}
				this.$api.cancelOrder(params).then(res => {
					uni.showToast({
						title:"取消成功",
						icon:"none"
					})
					this.refresh()
				}).catch(err=>{
					console.log("err",err)
				})
			},
			// 退款
			orderRefundOrder(item){
				let params = {
					order_id : item.order_id
				}
				uni.showModal({
					title:"温馨提示",
					content:"您正在发起退款，请您确认退款成功之后将不再获得每日收益，您购买的资金将退回您的账户积分余额中，是否确定？",
					complete: (res) => {
						if(res.confirm){
							this.$api.orderRefundOrder(params).then(res => {
								console.log(res)
								uni.showToast({
									title:"退款成功",
									icon:"none"
								})
								this.refresh()
							}).catch(err=>{
								console.log("err",err)
							})
						}
					}
				})
				
			},
			// 我的购买
			getMylist() {
				this.$api.mylist({
					...this.paging,
					status: this.currentStatus,
					type: 1
				}).then(res => {
					console.log(res.data)
					this.myList = [...this.myList, ...res.data.list]
					this.total = res.data.total
					this.myList.map(item=>{
						item.avatar = this.memberHome.avatar?this.memberHome.avatar : this.avatar
					})
				}).catch(err=>{
					console.log("err",err)
				})
			},
			// 我的团队
			geMyteamlist(type) {
				console.log({
					...this.paging,
					level:this.currentTeam,
					type: 1
				})
				
				this.$api.myteamlist({
					...this.paging,
					level:this.currentTeam,
					type: 1
				}).then(res => {
					console.log(res.data)
					this.myteamlist = [...this.myteamlist, ...res.data.list]
					this.total = res.data.total
					
				}).catch(err=>{
				})
			},
			// 刷新列表
			refresh() {
				this.paging.page = 1
				this.total = null
				if (this.navType == 1) {
					this.myList = []
					this.getMylist()
				} else {
					this.myteamlist = []
					this.geMyteamlist()
				}
			},
		}
	}
</script>

<style scoped lang="scss">
	.top {
		padding-top: 80rpx
	}

	.content {
		background: url('@/static/img/bg.png')no-repeat center/cover;
		min-height: 100vh;
		z-index: -1;

		.box {
			padding: 0 30rpx;
			padding-bottom: 30rpx;

			.nav {
				height: 120rpx;
				background: #FFFFFF;
				border-radius: 20rpx;
				display: flex;
				justify-content: space-around;

				.nav-list {
					font-size: 36rpx;
					// width: 140rpx;
					// height: 114rpx;
					text {
						line-height: 120rpx;
						padding-bottom: 30rpx;
					}
				}

				.nav-list-active {
					text {
						border-bottom: 7rpx #D06122 solid;
					}
				}
			}
			
			.stauts-nav{
				margin: 20rpx 0 ;
				height: 80rpx;
				display: flex;
				justify-content: space-between;
				font-size: 30rpx;
				.status-item{
					flex: 1;
					line-height: 80rpx;
					text-align: center;
					background: rgba(#fff,0.6);
				}
				.status-active{
					background: #D06122;
					color: #fff;
				}
			}
			
			
			.subscription-list {
				.subscription-item {
					padding: 0 30rpx;
					padding-top: 38rpx;
					margin-top: 16rpx;
					background: #FFFFFF;
					border-radius: 20rpx;
					.btn-buy{
						background: #D06122;
						color: #fff;
						width: 180rpx;
						height: 60rpx;
						line-height: 60rpx;
						text-align: center;
						font-size: 26rpx;
						font-weight: bold;
						border-radius: 6rpx;
					}
					.cancel-btn{
						background: #908d94;
					}
					.item-top {
						display: flex;
						padding-bottom: 32rpx;
						border-bottom: 1rpx solid #eaeaea;

						.item-top-img {
							width: 180rpx;
							position: relative;
							.status-box{
								z-index: 99;
								position: absolute;
								left: 10rpx;
								top: 16rpx;
								background: #3939ad;
								color: #fff;
								padding: 6rpx 14rpx;
								text-align: center;
								font-size: 28rpx;
								font-weight: bold;
								border-radius: 6rpx;
							}
							.status-4,.status-3{
								background: #989898;
							}
							.status-2{
								background: #D06122;
							}
						}

						.item-top-text {
							flex: 1;
							padding-left: 20rpx;
							display: flex;
							flex-direction: column;
							justify-content: space-between;

							.text-top {
								.text-top-title {
									font-family: PingFangSC, PingFang SC;
									font-weight: 500;
									font-size: 36rpx;
									color: #111111;
									line-height: 44rpx;
									text-align: left;
									font-style: normal;
								}

								.text-top-time {
									font-family: PingFangSC, PingFang SC;
									font-weight: 600;
									font-size: 28rpx;
									color: #999999;
									line-height: 34rpx;
									text-align: left;
									font-style: normal;
								}
							}
							.flex-box{
								display: flex;
								justify-content: space-between;
								.right-box{
									
								}
							}
							
							.text-bottom {
								height: 45rpx;
								display: flex;
								.text-bottom-text,
								.text-bottom-amount {
									display: inline-block;
								}

								.text-bottom-text {
									font-family: PingFangSC, PingFang SC;
									font-weight: 600;
									font-size: 28rpx;
									color: #989898;
									line-height: 44rpx;
									text-align: left;
									font-style: normal;
								}

								.text-bottom-amount {
									font-family: PingFangSC, PingFang SC;
									font-weight: 600;
									font-size: 36rpx;
									color: #D06122;
									line-height: 44rpx;
									text-align: left;
									font-style: normal;
								}
							}
						}
					}

					.item-bottom {
						display: flex;
						justify-content: space-between;
						min-height: 100rpx;
						align-items: center;
						align-self: center;
						.item-bottom-left {
							display: flex;

							.left-avatar {
								// width: 60rpx;
								// height: 60rpx;
								// // background-color: lawngreen;
								// // border-radius: 100%;
							}

							.left-name {
								padding-left: 20rpx;
								font-family: PingFangSC, PingFang SC;
								font-weight: 500;
								font-size: 36rpx;
								color: #111111;
								text-align: left;
								font-style: normal;
								.text-title-lv {
									border-radius: 18rpx;
									font-family: PingFangSC, PingFang SC;
									font-weight: 500;
									font-size: 32rpx;
									font-style: normal;
									padding: 4rpx 10rpx;
									margin-left: 12rpx;
								}
								.one {
									background: #FCEED7;
									color: #D06122;
								}
								
								.two {
									background: #DCE6FC;
									color: #4383DF;
								}
							}
						}

						.item-bottom-right {
							display: flex;
							justify-content: center;
							align-items: center;
							align-self: center;
							text {
								font-family: PingFangSC, PingFang SC;
								font-weight: 400;
								font-size: 36rpx;
								color: #999999;
								line-height: 60rpx;
								text-align: left;
								font-style: normal;
								padding-right: 12rpx;
							}
						}
					}
				}
			}
		}
	}
</style>